'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/template/kh/myTemplate 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
    const { pageIndex = 1, pageSize = 100} = data;
    if (typeof pageIndex !== 'number' || pageIndex <= 0 || typeof pageSize !== 'number' ) {
      res = { code: 400, msg: '参数错误' }
      return res;
    }
    try {
      console.log(userInfo)
      let { rows: allTemplate } = await vk.baseDao.select({
        dbName: 'avatar-template', // 表名
        getCount: true, // 是否需要同时查询满足条件的记录总数量，默认false
        getMain: false, // 是否只返回rows数据，默认false
        pageIndex, // 当前第几页
        pageSize // 每页条数
      });
      let { rows: userTemplate } = await vk.baseDao.select({
        dbName: 'avatar-user-template',
        whereJson:{ // 条件
            userId: uid
        }
      });
      
      if (userInfo.is_svip === 1) {
        res.rows = allTemplate
      } else {
        let userTemplateIds = userTemplate.map(item => item.templateId)
        let perTemplate = allTemplate.filter(item => {
          return userTemplateIds.indexOf(item.template_id) > -1
        })
        res.rows = perTemplate
      }
      
    } catch(err){
      res = { code: 500, msg: "查询错误" };
    }
    
    // 业务逻辑结束-----------------------------------------------------------
    return res;
	}
}
